unit UPharmacyStockTransferSub;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, AdvPanel, cxGraphics,
  cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxContainer, cxEdit,
  dxSkinsCore, dxSkinBlack, dxSkinBlue, dxSkinBlueprint, dxSkinCaramel,
  dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide, dxSkinDevExpressDarkStyle,
  dxSkinDevExpressStyle, dxSkinFoggy, dxSkinGlassOceans, dxSkinHighContrast,
  dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky,
  dxSkinMcSkin, dxSkinMoneyTwins, dxSkinOffice2007Black, dxSkinOffice2007Blue,
  dxSkinOffice2007Green, dxSkinOffice2007Pink, dxSkinOffice2007Silver,
  dxSkinOffice2010Black, dxSkinOffice2010Blue, dxSkinOffice2010Silver,
  dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinTheAsphaltWorld, dxSkinsDefaultPainters, dxSkinValentine, dxSkinVS2010,
  dxSkinWhiteprint, dxSkinXmas2008Blue, cxGroupBox, DBAdvGlowNavigator,
  cxStyles, dxSkinscxPCPainter, cxCustomData, cxFilter, cxData, cxDataStorage,
  cxNavigator, Data.DB, cxDBData, cxGridLevel, cxClasses, cxGridCustomView,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid, UDMPharmacy,
  cxTextEdit, cxMaskEdit, cxDropDownEdit, cxLookupEdit, cxDBLookupEdit,
  cxDBLookupComboBox, Vcl.StdCtrls, Vcl.Mask, Vcl.DBCtrls, cxDBEdit, cxCalendar, UDMSettings,
  Vcl.ComCtrls, dxCore, cxDateUtils, RzPanel, RzButton, Vcl.Menus, cxButtons,
  cxCheckBox, cxSpinEdit, frxClass, frxDBSet;

type
  TFPharmacyStockTransferSub = class(TForm)
    AdvPanel1: TAdvPanel;
    AdvPanelStyler1: TAdvPanelStyler;
    cxGroupBox1: TcxGroupBox;
    AdvPanel2: TAdvPanel;
    Label5: TLabel;
    Label4: TLabel;
    cxDBTextEdit1: TcxDBTextEdit;
    RzToolbar1: TRzToolbar;
    RzToolButton1: TRzToolButton;
    RzSpacer1: TRzSpacer;
    RzSpacer2: TRzSpacer;
    RzToolButton2: TRzToolButton;
    DBAdvGlowNavigator1: TDBAdvGlowNavigator;
    cxGroupBox3: TcxGroupBox;
    AdvPanel4: TAdvPanel;
    cxDBDateEdit1: TcxDBDateEdit;
    cxButton1: TcxButton;
    RzSpacer3: TRzSpacer;
    RzSpacer4: TRzSpacer;
    RzToolButton3: TRzToolButton;
    Label1: TLabel;
    cxLookupComboBox1: TcxLookupComboBox;
    Label2: TLabel;
    cxDBLookupComboBox1: TcxDBLookupComboBox;
    cxGroupBox2: TcxGroupBox;
    cxGrid1DBTableView1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGrid1DBTableView1pricing_id: TcxGridDBColumn;
    cxGrid1DBTableView1from_dept: TcxGridDBColumn;
    cxGrid1DBTableView1add: TcxGridDBColumn;
    cxGrid1DBTableView1expiry: TcxGridDBColumn;
    cxGrid1DBTableView1qty: TcxGridDBColumn;
    cxGrid1DBTableView1qtyInStock: TcxGridDBColumn;
    cxGrid1DBTableView1qtyToTransfer: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    cxGrid2DBTableView1: TcxGridDBTableView;
    cxGrid2DBTableView1pharmacy_stock_transfer_id: TcxGridDBColumn;
    cxGrid2DBTableView1pricing_id: TcxGridDBColumn;
    cxGrid2DBTableView1qty: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    AdvPanel3: TAdvPanel;
    cxButton2: TcxButton;
    his_pharmacy_transfer_report: TfrxDBDataset;
    ReTransfer: TfrxReport;
    procedure cxButton1Click(Sender: TObject);
    procedure cxLookupComboBox1PropertiesEditValueChanged(Sender: TObject);
    procedure RzToolButton2Click(Sender: TObject);
    procedure RzToolButton1Click(Sender: TObject);
    procedure cxButton2Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure DBAdvGlowNavigator1BtnEdit(Sender: TObject);
    procedure RzToolButton3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FPharmacyStockTransferSub: TFPharmacyStockTransferSub;

implementation

{$R *.dfm}

procedure TFPharmacyStockTransferSub.cxButton1Click(Sender: TObject);
begin
  Self.AdvPanel2.Enabled := true;
  DMPharmacy.his_pharmacy_stock_transfer.Post;
  DMPharmacy.his_pharmacy_stock_transfer.RefreshRecord;
  Self.AdvPanel2.Enabled := false;
  Self.AdvPanel4.Enabled := True;
end;

procedure TFPharmacyStockTransferSub.cxButton2Click(Sender: TObject);
  var
    virtual_rec_count : Integer;
    i : integer;
begin
  if  DMPharmacy.virtual_pharmacy_stock_transfer_sub.RecordCount > 0 then
  begin
    DMPharmacy.virtual_pharmacy_stock_transfer_sub.First;
    virtual_rec_count := DMPharmacy.virtual_pharmacy_stock_transfer_sub.RecordCount;
    for i := 1 to virtual_rec_count do
    BEGIN
      if DMPharmacy.virtual_pharmacy_stock_transfer_sub.FieldByName('qty').Value > DMPharmacy.virtual_pharmacy_stock_transfer_sub.FieldByName('qty').Value then
      BEGIN
        ShowMessage('The ordered Quantity should be less than the quantity in stock');
      END
      ELSE
      BEGIN
        if DMPharmacy.virtual_pharmacy_stock_transfer_sub.FieldByName('add').Value = True then
        BEGIN
          DMPharmacy.his_pharmacy_stock_transfer_sub.Insert;
          DMPharmacy.his_pharmacy_stock_transfer_sub.FieldByName('pharmacy_stock_transfer_id').Value := DMPharmacy.virtual_pharmacy_stock_transfer_sub.FieldByName('pharmacy_stock_transfer_id').value;
          DMPharmacy.his_pharmacy_stock_transfer_sub.FieldByName('pricing_id').Value := DMPharmacy.virtual_pharmacy_stock_transfer_sub.FieldByName('pricing_id').value;
          DMPharmacy.his_pharmacy_stock_transfer_sub.FieldByName('qty').Value := DMPharmacy.virtual_pharmacy_stock_transfer_sub.FieldByName('qtyToTransfer').value;
          DMPharmacy.his_pharmacy_stock_transfer_sub.Post;
          DMPharmacy.his_pharmacy_stock_transfer_sub.RefreshRecord;
        END;
      END;
      DMPharmacy.virtual_pharmacy_stock_transfer_sub.Next;
    END;
  end;
  DMPharmacy.virtual_pharmacy_stock_transfer_sub.Clear;

  DMPharmacy.his_pharmacy_transfer_report.Refresh;
end;

procedure TFPharmacyStockTransferSub.cxLookupComboBox1PropertiesEditValueChanged(
  Sender: TObject);
  var stockID : Integer;
  var itemCount : Integer;
  var i :integer;
begin
  stockID := Self.cxLookupComboBox1.EditValue;
  DMPharmacy.his_pharmacy_stock_pharmacy_stock_sub_trans.Active := False;
  DMPharmacy.his_pharmacy_stock_pharmacy_stock_sub_trans.ParamByName('a').Value := stockID;
  DMPharmacy.his_pharmacy_stock_pharmacy_stock_sub_trans.Active := True;

  itemCount := DMPharmacy.his_pharmacy_stock_pharmacy_stock_sub_trans.RecordCount;
  DMPharmacy.virtual_pharmacy_stock_transfer_sub.Active := True;
  DMPharmacy.virtual_pharmacy_stock_transfer_sub.Clear;
  for i := 0 to itemCount - 1 do
  BEGIN
    DMPharmacy.virtual_pharmacy_stock_transfer_sub.Insert;
    DMPharmacy.virtual_pharmacy_stock_transfer_sub.FieldByName('pharmacy_stock_transfer_id').value := DMPharmacy.his_pharmacy_stock_transfer.FieldByName('pharmacy_stock_transfer_id').Value;
    DMPharmacy.virtual_pharmacy_stock_transfer_sub.FieldByName('pricing_id').value := DMPharmacy.his_pharmacy_stock_pharmacy_stock_sub_trans.FieldByName('pricing_id').Value;
    DMPharmacy.virtual_pharmacy_stock_transfer_sub.FieldByName('qtyToTransfer').value := '1';
    DMPharmacy.virtual_pharmacy_stock_transfer_sub.FieldByName('from_dept').value := DMPharmacy.his_pharmacy_stock_pharmacy_stock_sub_trans.FieldByName('dept_id').Value;
    DMPharmacy.virtual_pharmacy_stock_transfer_sub.FieldByName('add').value := False;
    DMPharmacy.virtual_pharmacy_stock_transfer_sub.FieldByName('expiry').value := DMPharmacy.his_pharmacy_stock_pharmacy_stock_sub_trans.FieldByName('expiry_date').Value;
    DMPharmacy.virtual_pharmacy_stock_transfer_sub.FieldByName('qty').value := DMPharmacy.his_pharmacy_stock_pharmacy_stock_sub_trans.FieldByName('total_unit_qty').Value;
    DMPharmacy.virtual_pharmacy_stock_transfer_sub.FieldByName('qtyInStock').value := DMPharmacy.his_pharmacy_stock_pharmacy_stock_sub_trans.FieldByName('qty_in_stock').Value;
    DMPharmacy.virtual_pharmacy_stock_transfer_sub.Post;
    DMPharmacy.his_pharmacy_stock_pharmacy_stock_sub_trans.Next;
  END;
    
end;

procedure TFPharmacyStockTransferSub.DBAdvGlowNavigator1BtnEdit(
  Sender: TObject);
begin
  DMPharmacy.his_pharmacy_stock_transfer.Edit;
  Self.AdvPanel2.Enabled := True;
end;

procedure TFPharmacyStockTransferSub.FormActivate(Sender: TObject);
begin
  DMPharmacy.his_pharmacy_stock.Refresh;
end;

procedure TFPharmacyStockTransferSub.RzToolButton1Click(Sender: TObject);
begin
  Self.AdvPanel2.Enabled := True;
  DMPharmacy.his_pharmacy_stock_transfer.Insert;
  DMPharmacy.virtual_pharmacy_stock_transfer_sub.Clear;

end;

procedure TFPharmacyStockTransferSub.RzToolButton2Click(Sender: TObject);
begin
  Self.AdvPanel2.Enabled := false;
  Self.AdvPanel4.Enabled := True;
end;

procedure TFPharmacyStockTransferSub.RzToolButton3Click(Sender: TObject);
begin
  Self.ReTransfer.ShowReport();
end;

end.
